package design.simdi.code.final_paper.site.panels.administrare;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.hibernate.criterion.Criterion;

import design.simdi.code.final_paper.core.daos.PersonDao;
import design.simdi.code.final_paper.core.entities.Person;
import design.simdi.code.final_paper.core.enums.UserTypes;
import design.simdi.code.final_paper.site.application.MyAuthenticatedWebSession;

/**
 * 
 * @author Marius Dumitriu - (Jun 25, 2010 - 6:58:31 PM)
 * 
 */
public class SortablePersonDataProvider extends SortableDataProvider<Person> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SpringBean
	private PersonDao personDao;

	private UserTypes tipUtilizator;

	private List<Criterion> criterii;

	/**
	 * constructor
	 */
	public SortablePersonDataProvider() {
		InjectorHolder.getInjector().inject(this);
		// set default sort
		setTipUtilizator(UserTypes.STUDENT);
		setSort("nume", true);
		criterii = new ArrayList<Criterion>();

	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#iterator(int,
	 *      int)
	 */
	public Iterator<Person> iterator(int first, int count) {
		String sortQuery;
		SortParam sp = getSort();
		// Order o;
		if (sp.isAscending())
			sortQuery = " order by " + sp.getProperty() + " asc";
		else
			sortQuery = " order by " + sp.getProperty() + " desc";
		// return getPersonDao().findAll(new Order[] { o },
		// criterii).subList(first, first + count).iterator();

		String query = "";
		String subquery1 = MyAuthenticatedWebSession.get().isSignedIn() == true ? " " : " and user.status != 0 ";
		switch (tipUtilizator) {
		case STUDENT:
			query = String
					.format("select distinct person from Person as person , User as user, Student as student where "
							+ "person.student is not null and person.student = student and person.user = user and user.status !=3 "
							+ subquery1 + sortQuery);
			break;
		case ABSOLVENT:
			query = String
					.format("select distinct person from Person as person , User as user, Absolvent as absolvent where "
							+ "person.absolvent is not null  and person.absolvent = absolvent and person.user = user and user.status !=3 "
							+ subquery1 + sortQuery);

			break;
		case PROFESOR:
			query = String
					.format("select distinct person from Person as person , User as user, Profesor as profesor where "
							+ "person.profesor is not null and person.profesor = profesor and person.user = user and user.status !=3 "
							+ subquery1 + sortQuery);

			break;
		}

		List<Person> lista = personDao.find(query);

		return lista.subList(first, first + count).iterator();

	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#size()
	 */
	public int size() {
		// return getPersonDao().findAll(new Order[] {}, criterii).size();
		String sortQuery = " ";
		String query = "";
		String subquery1 = MyAuthenticatedWebSession.get().isSignedIn() == true ? " " : " and user.status != 0 ";
		switch (tipUtilizator) {
		case STUDENT:
			query = String
					.format("select distinct person from Person as person , User as user, Student as student where "
							+ "person.student is not null and person.student = student and person.user = user and user.status !=3 "
							+ subquery1 + sortQuery);
			break;
		case ABSOLVENT:
			query = String
					.format("select distinct person from Person as person , User as  user, Absolvent as  absolvent where "
							+ "person.absolvent is not null  and person.absolvent = absolvent and person.user = user and user.status !=3 "
							+ subquery1 + sortQuery);

			break;
		case PROFESOR:
			query = String
					.format("select distinct person from Person as person , User as user, Profesor as profesor where "
							+ "person.profesor is not null and person.profesor = profesor and person.user = user and user.status !=3 "
							+ subquery1 + sortQuery);

			break;
		}
		List<Person> lista = personDao.find(query);
		return lista.size();
	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#model(java.lang.Object)
	 */
	public IModel<Person> model(Person object) {
		return new DetachablePersonModel(object, getPersonDao());
	}

	public void setCriterii(List<Criterion> criterii) {
		this.criterii = criterii;
	}

	public List<Criterion> getCriterii() {
		return criterii;
	}

	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	public PersonDao getPersonDao() {
		return personDao;
	}

	public void setTipUtilizator(UserTypes tipUtilizator) {
		this.tipUtilizator = tipUtilizator;
	}

	public UserTypes getTipUtilizator() {
		return tipUtilizator;
	}

}